<?
class Pager{
	// input values
	public $id;
	public $limit;
	public $offset;
	public $count;
	public $pages_count;
	public $current_page;
	public $club_mode = false; // reverse pager - default page is last page
	
	// result values
	public $range;
	public $prev = 0;
	public $next = 0;
	
	
  function invert()
  {
    $this->offset = ($this->pages_count - $this->current_page) * $this->limit;
  }
  
	
	public function __construct($count, $id='page' , $limit = 5) {
			$this->count = $count;
			$this->id = $id;
			$this->limit = $limit;
			
			$this->pages_count = ceil($this->count / $this->limit);
			
			$this->current_page = ( isset($_GET[$id])&& is_numeric($_GET[$id]) ? intval($_GET[$id]) : 1 );
			if($this->current_page>$this->pages_count ) $this->current_page=$this->pages_count;
			if($this->current_page<1 ) $this->current_page=1;
			
			$this->offset = ($this->current_page - 1) * $this->limit;
			
			if($this->current_page > 1) $this->prev = Array('num'=>($this->current_page - 1), 'url'=>queryBuilder($this->id.'='.($this->current_page - 1)));
			if($this->current_page < $this->pages_count) $this->next =  Array('num'=>($this->current_page + 1), 'url'=>queryBuilder($this->id.'='.($this->current_page + 1)));
	}
	
	
	function getRange($pages_visible=5)
	{
			if($this->pages_count <= $this->pages_on_page) 
			{
				$range = range(1, $this->pages_count );
			}
			else
			{
				$half=floor($pages_visible / 2);
				$left = $this->current_page-1;
				$right = $this->pages_count - $this->current_page;
				
				if($left<=$half)
				{
					$show_left=$left;
					$show_right= min($right,$pages_visible - $left - 1);
				}
				elseif($right<=$half)
				{
					$show_right=$right;
					$show_left= min($left,$pages_visible - $right - 1);
				}
				else
				{
					$show_right=$half;
					$show_left=$half;
				}
				
				$range = range( $this->current_page-$show_left, $this->current_page+$show_right );
				
				if( !in_array(1,$range) ) $first=1;
				if( !in_array($this->pages_count,$range) ) $last=$this->pages_count;
			}
			
			$res_range = Array();
			
			if($first)
			{
				if($first>1 || $this->club_mode)
				{
					$res_range[] = Array('num'=>$first, 'url'=>queryBuilder($this->id.'='.$first));
				}
				else
				{
					$res_range[] = Array('num'=>$first, 'url'=>queryBuilder($this->id.'='));
				}
			}
			if($range[0]>2) 
			{
				$res_range[] = Array('num'=>'', 'url'=>'');
			}
			
			foreach($range as $page)
			{
				if($page>1 || $this->club_mode)
				{
					$res_range[] = Array('num'=>$page, 'url'=>queryBuilder($this->id.'='.$page));
				}
				else
				{
					$res_range[] = Array('num'=>$page, 'url'=>queryBuilder($this->id.'='));
				}
			}
			
			if($range[sizeof($range)-1] < $this->pages_count-1 ) 
			{
				$res_range[] = Array('num'=>'', 'url'=>'');
			}
			if($last)
			{
				$res_range[] = Array('num'=>$last, 'url'=>queryBuilder($this->id.'='.$last));
			}
		
		return $res_range;
	}
	
	
	
	function getExtraUrl()
	{
		return queryBuilder($this->id.'=extrapagenum');
	}
	
	
	
	function getSimpleRange($pages_visible=5)
	{
			if($this->pages_count <= $this->pages_on_page) 
			{
				$range = range(1, $this->pages_count );
			}
			else
			{
				$half=floor($pages_visible / 2);
				$left = $this->current_page-1;
				$right = $this->pages_count - $this->current_page;
				
				if($left<=$half)
				{
					$show_left=$left;
					$show_right= min($right,$pages_visible - $left - 1);
				}
				elseif($right<=$half)
				{
					$show_right=$right;
					$show_left= min($left,$pages_visible - $right - 1);
				}
				else
				{
					$show_right=$half;
					$show_left=$half;
				}
				
				$range = range( $this->current_page-$show_left, $this->current_page+$show_right );
				
				if( !in_array(1,$range) ) $first=1;
				if( !in_array($this->pages_count,$range) ) $last=$this->pages_count;
			}
			
			$res_range = Array();
			
			if($first)
			{
				if($first>1)
				{
					$res_range[] = Array('num'=>$first, 'url'=>queryBuilder($this->id.'='.$first));
				}
				else
				{
					$res_range[] = Array('num'=>$first, 'url'=>queryBuilder($this->id.'='));
				}
			}
			if($range[0]>2) 
			{
				$res_range[] = Array('num'=>'', 'url'=>'');
			}
			
			foreach($range as $page)
			{
				if($page>1)
				{
					$res_range[] = Array('num'=>$page, 'url'=>queryBuilder($this->id.'='.$page));
				}
				else
				{
					$res_range[] = Array('num'=>$page, 'url'=>queryBuilder($this->id.'='));
				}
			}
			
		return $res_range;
	}
	
	
	
	
	
	
	
	function getRangeAll()
	{
		$step = 1;
    if($this->pages_count>100) $step=10;
    if($this->pages_count>1000) $step=100;
    if($this->pages_count>10000) $step=200;
    $range = range(1, $this->pages_count , $step);
		
		$res_range = Array();
		foreach($range as $page)
    {
      if($step>1 && $page>1) $page--;
			$res_range[] = Array('num'=>$page, 'url'=>queryBuilder($this->id.'='.$page));
    }
		
		return $res_range;
	}
	
	
	function getString()
	{
		return ' limit '.$this->offset.','.$this->limit;
	}
	
	
	function __toString()
	{
		return $this->getString();
	}
}

